<?php
namespace app\houtai\controller\user\agent;
use app\houtai\controller\BaseController;

class BindController extends BaseController {

  /**
   *  @brief  : 玩家绑定查询
   *  @return : page
   *  @details: 
   */
  public function indexAction() {
    $conds = [];
    $order = [];
    $CHANNELS  = $this->GetAdminChannel();
    $GAME_KIND = GetGameKind();
    //////////////////////////////////////////////////////////////
    $Channel = $this->request->param('Channel/d', -1);
    if ($Channel > 0 && array_key_exists($Channel, $CHANNELS)) {
      $conds['c.Channel'][] = ['IN', array_keys($CHANNELS)];
      $conds['c.Channel'][] = ['EQ', $Channel];
    } else {
      $conds['c.Channel'] = ['IN', array_keys($CHANNELS)];
    }
    //////////////////////////////////////////////////////////////
    $KindID = $this->request->param('KindID/d', -1);
//    if ($KindID > 0 && array_key_exists($KindID, $GAME_KIND)) {
//      //$conds['a.KindID'] = $KindID;
//    }
    //////////////////////////////////////////////////////////////
    $time1 = $this->request->param('time1/s', '', 'trim');
    $time2 = $this->request->param('time2/s', '', 'trim');
    $aa = YmdHis2Unix($time1);
    $bb = YmdHis2Unix($time2);
    $ta = YmdHis($aa);
    $tb = YmdHis($bb);
    if ($aa > 0) {
      if ($bb > 0) {
        if ($aa <= $bb) {
          $conds['a.SpreaderTime'] = ['BETWEEN TIME', [$ta, $tb]];
        } else {
          $conds['a.SpreaderTime'] = ['>= TIME', $ta];
        }
      } else {
        $conds['a.SpreaderTime'] = ['>= TIME', $ta];
      }
    } else {
      if ($bb > 0) {
        $conds['a.SpreaderTime'] = ['<= TIME', $tb];
      }
    }
    ////////////////////////////////////////////////////////////////////////////
    $search   = $this->request->param('search/s', '', 'trim');
    $search2  = $this->request->param('search2/s', '', 'trim');
    if (!empty($search)) {
      if (is_numeric($search) && $search > 0 && $search < PHP_INT_MAX) {
        $search = $this->request->param('search/d', 0);
        if ($search > 0) $conds['c.GameID'] = $search;
      } else {
        $conds['c.RegisterIP|c.LastLogonIP|c.LastLogonMachine'] = ['LIKE', "%{$search}%"];
      }
    }
    if (!empty($search2)) {
      if (is_numeric($search2) && $search2 > 0 && $search2 < PHP_INT_MAX) {
        $search2 = $this->request->param('search2/d', 0);
        if ($search2 > 0) $conds['e.GameID'] = $search2;
      }
    }
    ////////////////////////////////////////////////////////////////////////////
    $orderMap = [
      'A' => 'ID',
      'B' => 'Channel',
      'C' => 'GameID',
      'D' => 'AGameID',
      'E' => 'BindTime',
      'F' => 'RoomCard',
      'G' => 'PayALL',
      'H' => 'PlayCount',
      'I' => 'OverCount',
    ];
    $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'F';
    $order[$orderMap[$this->orderField]] = $this->orderDirection;
    ////////////////////////////////////////////////////////////////////////////
    $list = table('CYBasicDB.dbo.AccountsGameCurrency a', 'rydb.basic')->field([
      'a.ID', 'a.UserID', 'a.RoomCard', 'c.SpreaderID' => 'AUserID',
      'c.SpreaderTime' => 'BindTime', 'c.GameID', 'c.NickName', 'c.Accounts',
      'c.Compellation', 'c.PassPortID', 'c.Sex', 'c.RegisterIP', 'c.RegisterDate',
      'c.LastLogonDate', 'c.LastLogonIP', 'c.Channel', 'c.AccountsType', 'c.GameLogonCount',
      'c.LastLogonMachine', 'c.RegisterMobile', 'c.AccountImgUrl' => 'Avatar',
      'ISNULL(e.GameID, 0)' => 'AGameID', "ISNULL(e.Compellation, '')" => 'ACompellation',
      'ISNULL(e.AccountsType, 0)' => 'AAccountsType', "ISNULL(e.AccountImgUrl, '')" => 'AAvatar',
      '(SELECT ISNULL(SUM(g.RMB), 0) FROM CYWebSMSDB.dbo.ChargeOnline g WHERE g.UserID = a.UserID AND g.Status = 1 AND g.GameStatus = 1)' => 'PayALL',
      '(SELECT COUNT (g.RecordID) FROM CYRecordDB.dbo.RecordCreateTable g WHERE g.UserID = a.UserID AND g.RecordType = 6 )' => 'PlayCount',
      '(SELECT COUNT (g.RecordID) FROM CYRecordDB.dbo.RecordCreateTable g WHERE g.UserID = a.UserID AND g.RecordType = 4 )' => 'OverCount'
    ])
    ->join('CYBasicDB.dbo.AccountsInfo c',     'c.UserID = a.UserID', 'LEFT')
    ->join('CYBasicDB.dbo.AccountsInfo e',     'e.UserID = c.SpreaderID', 'LEFT')
    ->where($conds)->order($order)->paginate();
    ////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'search'    => $search,
      'search2'   => $search2,
      'time1'     => $time1,
      'time2'     => $time2,
      'list'      => $list,
      'CHANNELS'  => $CHANNELS,
      'Channel'   => $Channel,
      'GAME_KIND' => $GAME_KIND,
      'KindID'    => $KindID,
    ]);
  }
  /**
   *  @brief  : 玩家绑定统计
   *  @return : page
   *  @details: 
   */
  public function trendAction() {
    $info = [];
    //$info['Month'] = table('RYAccountsDB.dbo.AccountsInfo a', 'rydb.accounts')->join('RYWebSMSDB.dbo.AgentReg e', 'a.SpreaderID = e.UserID')->whereTime('a.SpreaderIDBindTime', 'month')->count();
    //$info['Week']  = table('RYAccountsDB.dbo.AccountsInfo a', 'rydb.accounts')->join('RYWebSMSDB.dbo.AgentReg e', 'a.SpreaderID = e.UserID')->whereTime('a.SpreaderIDBindTime', 'week')->count();
    //$info['Today'] = table('RYAccountsDB.dbo.AccountsInfo a', 'rydb.accounts')->join('RYWebSMSDB.dbo.AgentReg e', 'a.SpreaderID = e.UserID')->whereTime('a.SpreaderIDBindTime', 'today')->count();
    ////////////////////////////////////////////////////////////////////////////
  }
}
